#include<vector>

#include"memorypool.hpp"
#include"log.hpp"

void print_total_size()
{
    int total_size=Memorypool::get_instance().get_total_size_kb();
    DBG("total size of memorypool is %d\n",total_size);
}

void print_left_size()
{
    int left_size=Memorypool::get_instance().get_left_size_kb();
    DBG("left size of memorypool is %d\n",left_size);
}

void print_list_size()
{
    int list_size;
    for(int i=mlow;i<=mup;i*=4)
    {
        list_size=Memorypool::get_instance().get_list_size_byte((MEM_CAP)i);
        DBG("total size of memorypool's %dKB is %dKB\n",i/1024,list_size/1024);
    }
}

void print_list_content()
{
    for(int i=mlow;i<mup;i*=4){Memorypool::get_instance().printf_list_content((MEM_CAP)i);}
}


void pop_full(Chunkptr& sp_chunk,std::vector<Chunkptr>& chunks,MEM_CAP index,int num)
{
    for(int i=0;i<num;i++)
    {
        Chunkptr tmp=Memorypool::get_instance().pop(index);
        tmp->copy(sp_chunk);
        chunks.push_back(tmp);
    }
}

void push_chunks(std::vector<Chunkptr>& chunks)
{
    for(auto e:chunks)
    {
        Memorypool::get_instance().push(e);
    }
}

int main()
{
    Log::get_instance()->init(nullptr);

    Log::get_instance()->set_log_level(Log::DEBUG_LOG_LEVEL);

    DBG("===================before pop...\n");
    print_total_size();
    print_left_size();
    print_list_size();
    print_list_content();

    std::vector<Chunkptr> chunks;

    Chunkptr sp_chunk=std::make_shared<Chunk>(10);
    std::string s="hello";
    strcpy(sp_chunk->_data,s.c_str());
    sp_chunk->_length=strlen(s.c_str());

    DBG("===================start pop...\n");
    pop_full(sp_chunk,chunks,m4K,1000);
    DBG("pop 1000 chunks,chunk size:%dKB\n",m4K/1024);

    pop_full(sp_chunk,chunks,m16K,1000);
    DBG("pop 1000 chunks,chunk size:%dKB\n",m16K/1024);

    pop_full(sp_chunk,chunks,m64K,500);
    DBG("pop 500 chunks,chunk size:%dKB\n",m64K/1024);

    pop_full(sp_chunk,chunks,m256K,30);
    DBG("pop 30 chunks,chunk size:%dKB\n",m256K/1024);

    pop_full(sp_chunk,chunks,m1M,30);
    DBG("pop 30 chunks,chunk size:%dKB\n",m1M/1024);

    pop_full(sp_chunk,chunks,m4M,30);
    DBG("pop 30 chunks,chunk size:%dKB\n",m4M/1024);

    DBG("===================after pop...\n");
    print_total_size();
    print_left_size();
    print_list_size();
    print_list_content();

    DBG("===================start push...\n");
    push_chunks(chunks);

    DBG("===================after push...\n");
    print_total_size();
    print_left_size();
    print_list_size();
    print_list_content();

    return 0;
}


